# TOKEN=token-01
# CLUSTER_STATE=new

# ETCD_NAME_1=etcd-01
# ETCD_NAME_2=etcd-02
# ETCD_NAME_3=etcd-03

# HOST_1=192.168.1.115
# HOST_2=192.168.1.116
# HOST_3=192.168.1.117

# PORT_API=2379
# PORT_PEER=2380

# HOST_NAME_1=cn5
# HOST_NAME_2=cn6
# HOST_NAME_3=cn7

# ETCD_HOME=/home/lorry/cn_shell/software/etcd-v3.5.11-linux-amd64
# #CLUSTER=etcd-01=https://cn1:2380,etcd-02=https://cn2:2380,etcd-03=https://cn3:2380
# CLUSTER=${ETCD_NAME_1}=http://${HOST_1}:${PORT_PEER},${ETCD_NAME_2}=http://${HOST_2}:${PORT_PEER},${ETCD_NAME_3}=http://${HOST_3}:${PORT_PEER}

# ETCD_NAMES=($ETCD_NAME_1 $ETCD_NAME_2 $ETCD_NAME_3)
# HOST_IPS=($HOST_1 $HOST_2 $HOST_3)
# HOST_NAMES=(${HOST_NAME_1}, ${HOST_NAME_2}, ${HOST_NAME_3})
# INDEXES=(1 2 3)
# for i in "${INDEXES[@]}"; do
# 	echo "*** i: $i"
# 	# For every machine
# 	THIS_NAME=${ETCD_NAMES[$i-1]}
# 	HOST_NAME=${HOST_NAMES[$i-1]}
# 	THIS_IP=${HOST_IPS[$i-1]}
# 	THIS_PORT_API=${PORT_API}
# 	THIS_PORT_PEER=${PORT_PEER}
# 	echo "start etcd in [$HOST_NAME::$THIS_NAME::$THIS_IP]"
# 	# kill etcd进程，如果已经存在的话
# 	# ssh lorry@cn$i lsof -i:$PORT_API | grep IP | awk '{print $2}' | xargs kill -9
# 	# ssh lorry@cn$i lsof -i:$PORT_PEER | grep IP | awk '{print $2}' | xargs kill -9
# 	# 拉起一个nohup的etcd进程
# 	ssh lorry@${HOST_NAME} nohup ${ETCD_HOME}/etcd --data-dir=${ETCD_HOME}/data.${THIS_NAME} \
# 		--name ${THIS_NAME} \
# 		--initial-advertise-peer-urls http://${THIS_IP}:${THIS_PORT_PEER} \
# 		--listen-peer-urls http://${THIS_IP}:${THIS_PORT_PEER} \
# 		--advertise-client-urls https://${THIS_IP}:${THIS_PORT_API} \
# 		--listen-client-urls https://${THIS_IP}:${THIS_PORT_API} \
# 		--client-cert-auth \
# 		--trusted-ca-file=/home/lorry/cn_shell/software/etcd-v3.5.11-linux-amd64/pki/etcd/ca.crt \
# 		--cert-file=/home/lorry/cn_shell/software/etcd-v3.5.11-linux-amd64/pki/apiserver-etcd-client.crt \
# 		--key-file=/home/lorry/cn_shell/software/etcd-v3.5.11-linux-amd64/pkiapiserver-etcd-client.key \
# 		--initial-cluster CLUSTER=etcd-01=https://${HOST_NAME_1}:2380,etcd-02=https://${HOST_NAME_2}:2380,etcd-03=https://${HOST_NAME_3}:2380 \
# 		--initial-cluster-state ${CLUSTER_STATE} > etcd.log &

# # ssh lorry@cn1 nohup /home/lorry/cn_shell/software/etcd-v3.5.11-linux-amd64/etcd --data-dir=/home/lorry/cn_shell/software/etcd-v3.5.11-linux-amd64/data.etcd-01 --name etcd-01 \
# # --initial-advertise-peer-urls http://192.168.1.165:2380 --listen-peer-urls http://192.168.1.165:2380 \
# # --advertise-client-urls http://192.168.1.165:2379 --listen-client-urls http://192.168.1.165:2379 \
# # --client-cert-auth \
# # --trusted-ca-file=/home/lorry/cn_shell/software/etcd-v3.5.11-linux-amd64/pki/etcd/ca.crt \
# # --cert-file=/home/lorry/cn_shell/software/etcd-v3.5.11-linux-amd64/pki/apiserver-etcd-client.crt \
# # --key-file=/home/lorry/cn_shell/software/etcd-v3.5.11-linux-amd64/pkiapiserver-etcd-client.key \
# # --initial-cluster CLUSTER=etcd-01=https://cn1:2380,etcd-02=https://cn2:2380,etcd-03=https://cn3:2380 \
# # --initial-cluster-state new --initial-cluster-token token-01 > /home/lorry/cn_shell/software/etcd-v3.5.11-linux-amd64/etcd.log &

# done

# ENDPOINTS=$HOST_1:${PORT_API},$HOST_2:${PORT_API},$HOST_3:${PORT_API}

# #./etcdctl --endpoints=cn1:2379,cn2:2379,cn3:2379 member list
# # 查看etcd集群成员
# ${ETCD_HOME}/etcdctl --endpoints=${ENDPOINTS} --write-out=table  member list
# # 查看集群成员状态
# ${ETCD_HOME}/etcdctl --endpoints=${ENDPOINTS} --write-out=table  endpoint status
# # 查看集群成员健康
# ${ETCD_HOME}/etcdctl --endpoints=${ENDPOINTS} --write-out=table  endpoint health

# # 测试写入/读取
# TEST_KEY=foo
# TEST_VALUE="Hello World"
# ${ETCD_HOME}/etcdctl --endpoints=${ENDPOINTS}  put ${TEST_KEY} ${TEST_VALUE}

# echo "expect key: [$TEST_KEY], expect value: [$TEST_VALUE]... ..."
# echo "${ETCD_HOME}/etcdctl --endpoints=${ENDPOINTS}  get ${TEST_KEY}"
# ${ETCD_HOME}/etcdctl --endpoints=${ENDPOINTS}  get ${TEST_KEY}